package com.comum.javahomework420242025.controller;

import com.comum.javahomework420242025.pojo.*;
import com.comum.javahomework420242025.repository.PayPhaseRepository;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class PayPhaseController {
    @Autowired
    private PayPhaseRepository payPhaseRepository;
    @PostMapping("/addPayPhase")
    public Result addPayPhase(String contractid,@RequestBody List<PayPhase> PayPhases){
        for(PayPhase a : PayPhases){
            a.setId(null);
            a.setContractid(Long.valueOf(contractid));
            payPhaseRepository.save(a);
        }
        return Result.success();
    }
    @PostMapping("/updatePayPhase")
    public Result updatePayPhase(String contractid,@RequestBody List<PayPhase> PayPhases){
        for(PayPhase a : PayPhases){
            if(a.getId()==null){
                a.setContractid(Long.valueOf(contractid));
            }
            payPhaseRepository.save(a);
        }
        return Result.success();
    }
    @DeleteMapping("/deletephase")
    public Result deletephase(String id){
        System.out.println(id);
        payPhaseRepository.deleteById(Long.valueOf(id));
        return Result.success();
    }
    @GetMapping("/getPayPhaseByContractId")
    public Result<List<PayPhase>> getPayPhaseByContractId(String contractid){
        List<PayPhase> res = payPhaseRepository.findAllByContractid(contractid);
        return Result.success(res);
    }
    @PostMapping("/getPayPhases")
    public Result<PageBean<PayPhase>> getPayPhases(
            @RequestBody AskList askList){
        int pageSize = askList.getPageSize();
        int pageNum = askList.getPageNum();
        String str = askList.getStr();
        PageBean<PayPhase> pb = new PageBean<>();
        Pageable pageable = PageRequest.of(pageNum-1,pageSize);
        Page<PayPhase> page = payPhaseRepository.getPayPhases(str,pageable);
        pb.setTotal(page.getTotalElements());
        pb.setItems(page.getContent());
        return Result.success(pb);
    }
    @PostMapping("/getPayPhasesForInquiry")
    public Result<PageBean<PayPhase>> getPayPhasesForInquiry(
            @RequestBody AskList askList){
        int pageSize = askList.getPageSize();
        int pageNum = askList.getPageNum();
        String str = askList.getStr();
        PageBean<PayPhase> pb = new PageBean<>();
        Pageable pageable = PageRequest.of(pageNum-1,pageSize);
        Page<PayPhase> page = payPhaseRepository.getPayPhasesForInquiry(str,pageable);
        pb.setTotal(page.getTotalElements());
        pb.setItems(page.getContent());
        return Result.success(pb);
    }
    @PutMapping("/updateoughttime")
    public Result updateoughttime(@RequestBody PayPhase payPhase){
        System.out.println(payPhase);
        payPhaseRepository.save(payPhase);
        return Result.success();
    }

}
